package com.yang.job.application.service;


import com.yang.job.application.convertor.YangJobDTOConvertor;
import com.yang.job.client.api.YangJobService;
import com.yang.job.client.dto.YangJobDTO;
import com.yang.job.client.dto.command.DeleteYangJobCommand;
import com.yang.job.client.dto.command.NewYangJobCommand;
import com.yang.job.client.dto.command.UpdateYangJobCommand;
import com.yang.job.client.dto.common.Response;
import com.yang.job.domain.enums.JobExecuteStrategyEnum;
import com.yang.job.domain.model.YangJobModel;
import com.yang.job.domain.gateway.repository.IJobModelRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class YangJobApplicationService implements YangJobService {
    @Resource
    private IJobModelRepository jobModelRepository;

    @Resource
    private YangJobDTOConvertor yangJobDTOConvertor;

    @Override
    public Response<YangJobDTO> saveYangJob(NewYangJobCommand newYangJobCommand) {
        YangJobModel yangJobModel = convert2YangJobModel(newYangJobCommand);
        if (jobModelRepository.saveYangJobModel(yangJobModel)) {
            // 发送事件
            yangJobModel.postSaveJobEvent();
            YangJobDTO yangJobDTO = yangJobDTOConvertor.convert2DTO(yangJobModel);
            return Response.success(yangJobDTO);
        }
        return Response.fail();
    }

    private YangJobModel convert2YangJobModel(NewYangJobCommand newYangJobCommand) {
        YangJobModel yangJobModel = new YangJobModel();
        yangJobModel.setJobName(newYangJobCommand.getJobName());
        yangJobModel.setDescription(newYangJobCommand.getDescription());
        yangJobModel.setCron(newYangJobCommand.getCron());
        yangJobModel.setOpen(newYangJobCommand.getOpen());
        yangJobModel.setExecuteStrategy(JobExecuteStrategyEnum.getJobExecuteStrategyByName(newYangJobCommand.getExecuteStrategy()));
        yangJobModel.setExecuteClassPath(newYangJobCommand.getExecuteClassPath());
        yangJobModel.setExecuteParamMap(newYangJobCommand.getParams());
        return yangJobModel;
    }

    @Override
    public Response<YangJobDTO> updateYangJob(UpdateYangJobCommand updateYangJobCommand) {
        YangJobModel yangJobModel = jobModelRepository.getYangJobModelById(updateYangJobCommand.getJobId());
        if (yangJobModel == null) {
            return Response.fail();
        }

        if (updateYangJobCommand.getJobName() != null) {
            yangJobModel.setJobName(updateYangJobCommand.getJobName());
        }
        if (updateYangJobCommand.getCron() != null) {
            yangJobModel.setCron(updateYangJobCommand.getCron());
        }
        if (updateYangJobCommand.getOpen() != null) {
            yangJobModel.setOpen(updateYangJobCommand.getOpen());
        }
        if (!updateYangJobCommand.getParams().isEmpty()) {
            yangJobModel.setExecuteParamMap(updateYangJobCommand.getParams());
        }
        if (updateYangJobCommand.getExecuteClassPath() != null) {
            yangJobModel.setExecuteClassPath(updateYangJobCommand.getExecuteClassPath());
        }
        if (updateYangJobCommand.getExecuteStrategy() != null) {
            yangJobModel.setExecuteStrategy(JobExecuteStrategyEnum.getJobExecuteStrategyByName(updateYangJobCommand.getExecuteStrategy()));
        }
        if (jobModelRepository.updateYangJobModel(yangJobModel)) {
           // 发送事件
            yangJobModel.postUpdateJobEvent();
            YangJobDTO yangJobDTO = yangJobDTOConvertor.convert2DTO(yangJobModel);
            return Response.success(yangJobDTO);
        }
        return Response.fail();
    }

    @Override
    public Response<YangJobDTO> deleteYangJob(DeleteYangJobCommand deleteYangJobCommand) {
        Integer jobId = deleteYangJobCommand.getJobId();
        if (jobModelRepository.deleteJobModel(jobId)) {
            YangJobModel yangJobModel = jobModelRepository.getYangJobModelById(jobId);
            yangJobModel.postDeleteJobEvent();
            return Response.success();
        }
        return Response.fail();
    }
}

